package com.resou.reader.mine.login;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.resou.reader.R;
import com.resou.reader.mine.userinfo.UserInfoActivity;
import com.resou.reader.utils.NetworkUtils;
import com.resou.reader.utils.ToastUtil;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by ${Garrett} on 2018/11/2.
 * Contact me krouky@outlook.com
 */
public class GenderChoiceFragment extends DialogFragment {

    private Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        final View view = inflater.inflate(R.layout.gender_choice_layout, container, false);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        Window window = getDialog().getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        super.onActivityCreated(savedInstanceState);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.horizontalMargin = getResources().getDimension(R.dimen.qb_px_23);
        window.setAttributes(layoutParams);
    }

    @OnClick({R.id.male_btn, R.id.female_btn})
    public void onClick(View v) {
        String gender;
        switch (v.getId()) {
            case R.id.male_btn:
                gender = "1";
                break;
            case R.id.female_btn:
                gender = "2";
                break;
            default:
                gender = "1";
                break;
        }
        if (!NetworkUtils.isNetworkConnected(getContext())) {
            ToastUtil.makeShortToast("网络异常");
            return;
        }
        if (getActivity() != null && !getActivity().isDestroyed())
            ((UserInfoActivity) getActivity()).updateGenderFromFragment(gender);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}
